import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22479
 * Date: 2022-12-01
 * Time: 11:54
 */

@WebServlet("/logout")
public class LogOutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //这个方法用于注销删除session

        //1.获取当前会话
        HttpSession session = req.getSession(false);
        if(session == null) {
            //无会话,当前是未登录状态
            resp.setStatus(403);
            resp.sendRedirect("login.html");
            return;
        }
        //这里的user对象需要判断是否存在
        User user = (User) session.getAttribute("user");
        if(user == null) {
            resp.setStatus(403);
            resp.sendRedirect("login.html");
            return;
        }
        //存在,删除session对应的user
        session.removeAttribute("user");
        resp.sendRedirect("login.html");

    }
}
